//#include <iostream>
//#include <sstream>
//#include <algorithm>
//#include <cmath>
//#include "lib/tinyxml2.h"
//#include "rearrangement_maps.h"

//using namespace tinyxml2;
//using namespace std;

//RearrangementMaps::RearrangementMaps() {

//}

//RearrangementMaps::RearrangementMaps(SegmentPairLibrary &segment_pair_library, string product_genome_filename) {
//  ifstream file(product_genome_filename);
//  string line;
//  while(getline(file, line))
//  {
//    stringstream linestream(line);
//    string data;
//    string product_contig_name;
//    int product_contig_length;

//    getline(linestream, product_contig_name, '\t');

//    linestream >> product_contig_length;
    
//    product_contig_lengths[product_contig_name] = product_contig_length;
//  }

//  int map_id = 0;
////  int coverage_percent[11];
////  for(int i = 0; i < 11; ++i) coverage_percent[i] = 0;
////  int total_maps = 0;
  
//  while(segment_pair_library.next()) {
//    string product_name = segment_pair_library.get_cur_product_name();
//    string precursor_name = segment_pair_library.get_cur_precursor_name();
//    int product_length = product_contig_lengths[product_name];
//    RearrangementMap rearrangement_map(map_id, precursor_name, product_name, product_length, segment_pair_library.get_segment_pairs());
    
////    int product_contig_coverage = rint(10*float(rearrangement_map.product_length)/product_contig_lengths[product_name]);
//    bool good_map = rearrangement_map.well_formed() && rearrangement_map.complete();
//    //if ( rearrangement_map.well_formed() ) cout << "found a well-formed map" << endl;
//    //cout << rearrangement_map.well_formed() << " " <<  rearrangement_map.complete() << endl;
//    //if (  !rearrangement_map.well_formed() && rearrangement_map.complete() ) cout << rearrangement_map;
//    //if ( rearrangement_map.complete() ) cout << "found a complete map";
//    //cout << rearrangement_map;
//    if(good_map) { //&& product_contig_coverage >= 9  ) {
//      //cout << "found a good map" << endl;
//      addMap(rearrangement_map);
//      //maps_for_precursor_mac[precursor_name][product_name] = rearrangement_map;
//      //maps_for_mac[product_name] = rearrangement_map;
//      //coverage_percent[product_contig_coverage]++;
//      //total_maps++;
//    }
    
//    map_id++;
//  }
//  //for(int i = 0; i < 11; ++i) cout << i*10 << " " << coverage_percent[i] << endl;
//  //cout << total_maps << endl;
//  //out << endl;
//}

////Get a reference to a map with the given precursor_name and product_name and return true;
////return false otherwise.
//bool RearrangementMaps::getMap(string precursor_name, string product_name, RearrangementMap &map) {
//  if ( maps_for_precursor_mac.find(precursor_name) != maps_for_precursor_mac.end() ) {
//    if ( maps_for_precursor_mac[precursor_name].find(product_name) != maps_for_precursor_mac[precursor_name].end()) {
//     map = maps_for_precursor_mac[precursor_name][product_name];
//     return true;
//    }
//  }
//  return false;
//}

//bool RearrangementMaps::getMapForMicId(string precursor_name, map<string, RearrangementMap> &maps) {
//  if ( maps_for_precursor_mac.find(precursor_name) != maps_for_precursor_mac.end() ) {
//     maps = maps_for_precursor_mac[precursor_name];
//     return true;
//  }
//  return false;
//}

//bool RearrangementMaps::getMap(string product_name, RearrangementMap &map) {
// if ( maps_for_mac.find(product_name) != maps_for_mac.end() ) {
//     map = maps_for_mac[product_name];
//     return true;
//  }
//  return false;
//}

//void RearrangementMaps::addMap(RearrangementMap& map) {
//  maps_for_precursor_mac[map.precursor_name()][map.product_name()] = map;
//  maps_for_mac[map.product_name()] = map;
//}

//bool RearrangementMaps::saveXML(string filename) {
//  XMLDocument doc;
//  XMLDeclaration *decl = doc.NewDeclaration();
//  doc.LinkEndChild( decl );
//  XMLElement *root = doc.NewElement("mapSet");
//  doc.LinkEndChild( root );
  
//  XMLElement *mapElement;
//  XMLElement *micElement;
//  XMLElement *macElement;
 
//  for(map< string, map<string, RearrangementMap> >::const_iterator map_for_precursor_mac = maps_for_precursor_mac.begin();
//      map_for_precursor_mac != maps_for_precursor_mac.end(); ++map_for_precursor_mac) {
//    map<string, RearrangementMap> const &maps_for_mac = (*map_for_precursor_mac).second;
//    for(map<string, RearrangementMap>::const_iterator product_map = maps_for_mac.begin(); product_map != maps_for_mac.end(); ++product_map) {
//    RearrangementMap const &map = (*product_map).second;
//    mapElement = doc.NewElement("map");
//    mapElement->SetAttribute("id", map.map_id());
//    mapElement->SetAttribute("complete", map.complete());
//    mapElement->SetAttribute("well_formed", map.well_formed());
//    mapElement->SetAttribute("scrambled", map.scrambled());
//    mapElement->SetAttribute("product_length", map.product_length());
//    root->LinkEndChild(mapElement);
//    micElement = doc.NewElement("mic");
//    mapElement->LinkEndChild(micElement);
//    micElement->LinkEndChild(doc.NewText(map.precursor_name().c_str()));
//    macElement = doc.NewElement("mac");
//    mapElement->LinkEndChild(macElement);
//    macElement->LinkEndChild(doc.NewText(map.product_name().c_str()));
//    XMLElement *iesElement;
//    XMLElement *micRegionElement;
//    for(auto ies = map.ies_vector_.begin(); ies != map.ies_vector_.end(); ++ies) {
//      iesElement = doc.NewElement("ies");
//      mapElement->LinkEndChild(iesElement);
//      micRegionElement = doc.NewElement("micRegion");
//      iesElement->LinkEndChild(micRegionElement);
//      stringstream ss;
//      ss << ies->precursor_region.start;
//      micRegionElement->SetAttribute("start", ss.str().c_str());
//      ss.str("");
//      ss << ies->precursor_region.end;
//      micRegionElement->SetAttribute("end", ss.str().c_str());
//      iesElement->SetAttribute("is_conventional", ies->is_conventional);
//    }
//    XMLElement *mdsElement;
//    XMLElement *macRegionElement;
//    for(auto mds = map.mds_vector_.begin(); mds != map.mds_vector_.end(); ++mds) {
//      mdsElement = doc.NewElement("mds");
//      mapElement->LinkEndChild(mdsElement);
//      mdsElement->SetAttribute("orientation", mds->is_reversed ? "-" : "+");
//      mdsElement->SetAttribute("micOrder", to_string(mds->precursor_order).c_str());
//      mdsElement->SetAttribute("macOrder", to_string(mds->product_order).c_str());
//      micRegionElement = doc.NewElement("micRegion");
//      mdsElement->LinkEndChild(micRegionElement);
//      stringstream ss;
//      ss << mds->precursor_region.start;
//      micRegionElement->SetAttribute("start", ss.str().c_str());
//      ss.str("");
//      ss << mds->precursor_region.end;
//      micRegionElement->SetAttribute("end", ss.str().c_str());
//      macRegionElement = doc.NewElement("macRegion");
//      mdsElement->LinkEndChild(macRegionElement);
//      ss.str("");
//      ss << mds->product_region.start;
//      macRegionElement->SetAttribute("start", ss.str().c_str());
//      ss.str("");
//      ss << mds->product_region.end;
//      macRegionElement->SetAttribute("end", ss.str().c_str());
//    }
//    }
//  }
 
//  doc.SaveFile(filename.c_str());
//  return true;
//}

//bool RearrangementMaps::loadXML(string filename) {
//  XMLDocument doc;
//  if( doc.LoadFile(filename.c_str()) == XML_SUCCESS ) {
//    XMLElement *mapElement;
//    for(mapElement = doc.RootElement()->FirstChildElement("map"); mapElement; mapElement = mapElement->NextSiblingElement("map")) {
//      int map_id = stoi(mapElement->Attribute("id"));
//      string product_name = mapElement->FirstChildElement("mac")->GetText();
//      string precursor_name = mapElement->FirstChildElement("mic")->GetText();
//      RearrangementMap map(map_id, precursor_name, product_name);
//      XMLElement *iesElement = mapElement->FirstChildElement("ies");
//      for( ; iesElement; iesElement = iesElement->NextSiblingElement("ies")) {
//        XMLElement *micRegion = iesElement->FirstChildElement("micRegion");
//        int precursor_start = stoi(micRegion->Attribute("start"));
//        int precursor_end = stoi(micRegion->Attribute("end"));
//        bool is_conventional = iesElement->Attribute("is_conventional");
//        map.addIes(precursor_start, precursor_end, is_conventional);
//      }
//      XMLElement *mdsElement = mapElement->FirstChildElement("mds");
//      for( ; mdsElement; mdsElement = mdsElement->NextSiblingElement("mds")) {
//        string orientation = mdsElement->Attribute("orientation");
//        bool isReversed = orientation.compare("-") == 0 ? true : false;
//        int micOrder = stoi(mdsElement->Attribute("micOrder"));
//        int macOrder = stoi(mdsElement->Attribute("macOrder"));
//        XMLElement *micRegion = mdsElement->FirstChildElement("micRegion");
//        int micStart = stoi(micRegion->Attribute("start"));
//        int micEnd = stoi(micRegion->Attribute("end"));
//        XMLElement *macRegion = mdsElement->FirstChildElement("macRegion");
//        int macStart = stoi(macRegion->Attribute("start"));
//        int macEnd = stoi(macRegion->Attribute("end"));
//        map.addMds(micStart, micEnd, macStart, macEnd, micOrder, macOrder, isReversed);
//      }
//      //maps.push_back(map);
//      addMap(map);
//    }
//  } else {
//    cerr << "Unable to load xml file" << endl;
//  }
//  return true;
//}
